<%--
    Document   : index
    Created on : 02 4, 12, 12:10:01 PM
    Author     : Martin Spencer Sy
--%>

<%@page import="java.io.UnsupportedEncodingException"%>
<%@page import="java.security.NoSuchAlgorithmException"%>
<%@page import="java.security.MessageDigest"%>
<%@page import="Controller.UserManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <%
    String user = (String)session.getAttribute("user"),
            name = (String)session.getAttribute("name");
    
    //if the user is logged in check for session integrity
    if( (String)session.getAttribute("user") != null ){
        
        //check session integrity by comparing the hash of the session token
        String currSession = request.getRemoteAddr().concat( session.getId() );
        try{
            if( !UserManager.checkSessionIntegrity(currSession, session) ){
                UserManager.logout(request);
                response.sendRedirect("login.jsp");
            }
        }catch(NoSuchAlgorithmException e1){
            UserManager.logout(request);
            response.sendRedirect("login.jsp");
        }
        catch(UnsupportedEncodingException e1){
            UserManager.logout(request);
            response.sendRedirect("login.jsp");
        }
        catch(Exception e1){
            UserManager.logout(request);
            response.sendRedirect("login.jsp");
        }
        
    }
    %>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
        <link rel="stylesheet" href="css/960_24_col.css" />
        <link rel="stylesheet" href="css/reset.css" />
        <link rel="stylesheet" href="css/text.css" />
        <link rel="stylesheet" href="css/style.css" />
        
        <title>FooBar Online Bookstore</title>
    </head>
    <body>
        <div id="bar"></div>
        <!-------------------------------header here----------------------->
            <div id="header-wrap">
                <div id="dashboard" class="container_24">
                    <div id="dashboard-logo" class="grid_7">
                        <a href="index.jsp"><img src="images/logo.png" alt="logo" /></a>
                    </div>
                    <div id="dashboard-menu" class="grid_17">
                        <ul class="menu" id="main-menu">
                            <li><a href="index.jsp">Home</a></li>
                            <li><a href="main.jsp">Products</a>
                                <ul style="display: none;">
                                    <li><a href="main.jsp?show=book">Books</a></li>
                                    <li><a href="main.jsp?show=mags">Magazines</a></li>
                                    <li><a href="main.jsp?show=cd">CD</a></li>
                                    <li><a href="main.jsp?show=dvd">DVD</a></li>
                                    <li><a href="main.jsp?show=acc">Accessories</a></li>
                                </ul>
                            </li>
                            <%
                            if( user != null && UserManager.isAdmin(user) == 0 && UserManager.isFinanceManager(user) == 0 && UserManager.isProductManager(user) == 0 ){
                                out.println("<li><a href='myProfile.jsp'>My Profile</a></li>");
                            }
                            else if(UserManager.isAdmin(user) == 1){
                                out.println("<li><a href='admin/adminPanel.jsp'>Admin Panel</a></li>");
                            }
                            else if(UserManager.isFinanceManager(user) == 1){
                                out.println("<li><a href='admin/financePanel.jsp'>Sales Panel</a></li>");
                            }
                            else if(UserManager.isProductManager(user) != 0){
                                out.println("<li><a href='admin/productPanel.jsp'>Product Panel</a></li>");
                            }
                            %>
                            <li><a href="about.jsp">About</a></li>
                            <li><a href="contact.jsp">Contact Us</a></li>
                            <%
                            if(user == null){
                                out.println("<li><a href='login.jsp'>Login</a> | <a href='register.jsp'>Join Now</a></li>");
                            }
                            else{
                                out.println("<li><form action='logout' name='logout' method='post'>"
                                + "<a id='user-header' href='myProfile.jsp'>" + name + "</a>"
                                + "<input type='submit' value='Logout' class='submit-button' />"
                                + "</form></li>");
                            }
                            %>
                        </ul>
                    </div>
                </div>
            </div>
        <!-------------------------------content here----------------------->
        <div id="content-wrap">
            <div id="content" class="container_24">
                <div id="metro-box1" class="metro-box grid_10">
                    <h3>Welcome to FooBar</h3>
                    <p>Shop in one of the most secure online bookstore ever!</p>
                </div>
                
                <%
                //if user is logged in
                if( user != null){
                %>
                
                <div id="metro-box2" class="metro-box grid_14">
                    <h3>Good Day <%out.println(name+"!");%></h3>
                    <p>
                        Feel free to take a look around <br />
                        Just buy anything you feel like buying no one is going to stop you.
                    </p>
                </div>
                
                <%
                }
                else{
                %>
                
                <div id="metro-box2" class="metro-box grid_14">
                    <div class="grid_5 omega">
                        <h3>Login</h3>
                        <p>
                            See something you want?<br />
                            Then login to buy it! <br />
                            Not yet a member? <br />
                            <a href='register.jsp'>Join Now!</a>
                        </p>
                    </div>
                    <div class="grid_8 prefix_1 alpha">
                        <form action='login' method='post' id="index-login-form">
                            <label>Username</label><br />
                            <input type='text' name='username' class="form-inputs" maxlength="25" AUTOCOMPLETE='off' /><br />
                            
                            <label>Password</label><br />
                            <input type='password' name='password' class="form-inputs" maxlength="25" AUTOCOMPLETE='off'/><br />
                            
                            <input type='submit' name='login' value='Login' class="submit-button" /><br />
                        </form>
                    </div>
                </div>
                
                <%
                }
                %>
                
                <div id="metro-box3" class="metro-box grid_24">
                    <h3>Featured Products</h3>
                    <ul id="index-product-list">
                        <li><a href="viewProduct.jsp?id=The_Hunger_Games">
                                <span>The Hunger Games</span>
                            </a>
                        </li>
                        <li><a href="viewProduct.jsp?id=Steve_Jobs">
                                <span>Steve Jobs</span>
                            </a>
                        </li>
                        <li><a href="viewProduct.jsp?id=Top_Gear">
                                <span>Top Gear</span>
                            </a>
                        </li>
                        <li><a href="viewProduct.jsp?id=Lord_of_The Rings:_The_Two_Towers">
                                <span>Lord of The Rings: The Two Towers</span>
                            </a>
                        </li>
                    </ul>
                    <a href="main.jsp">See all products>></a>
                </div>
                
                <div id="metro-box4" class="metro-box grid_12">
                    <h3>About</h3>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
                    consequat. <a href="about.jsp">Read More>></a></p>
                </div>
                
                <div id="metro-box5" class="metro-box grid_12">
                    <h3>Contact Us</h3>
                    <p>Have some comments and suggestions you want to share with us?<br /> 
                        Then visit our contact us page. <br />
                        Don't hesitate to send us a message.
                    </p>
                    <a href="about.jsp">Send us a message>></a>
                </div>
            </div>
        </div>
        <!-------------------------------footer here----------------------->
        <div id="upper-footer-wrap">
            <div id="upper-footer" class="container_24">
                <div class="grid_7 prefix_17">
                    <ul class="menu">
                        <li><a href="#">top</a></li>
                        <li><a href="index.jsp">home</a></li>
                        <li><a href="contact.jsp">contact us</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div id="lower-footer-wrap">
            <div id="lower-footer" class="container_24">
                <center>
                    <p>FooBar Online Bookstore SECPROG Project</p>
                </center>
            </div>
        </div>
    </body>
</html>
